home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Rkrm / Graphics_Libraries / Sprites_Bobs / animtools.e next >
Encoding:
Text File  |  1995-09-20  |  12.8 KB  |  357 lines

  1. -> animtools.e
  2. ->
  3. -> This file is a collection of tools which are used with the VSprite, Bob and
  4. -> Animation system software.  It is intended as a useful EXAMPLE, and while it
  5. -> shows what must be done, it is not the only way to do it.  If Not Enough
  6. -> Memory, or error return, each cleans up after itself before returning.
  7. -> NOTE that these routines assume a very specific structure to the GEL lists.
  8. -> Make sure that you use the correct pairs together
  9. -> (i.e., makeOb()/freeOb())
  10.  
  11. ->>> Header (globals)
  12. OPT MODULE, PREPROCESS
  13. OPT EXPORT
  14.  
  15. MODULE 'exec/memory',
  16.        'graphics/gels',
  17.        'graphics/gfx',
  18.        'graphics/rastport'
  19.  
  20. -> 'intuition/intuitionbase' does not define a library name.
  21. #define INTUITIONNAME 'intuition.library'
  22.  
  23. -> These objects are used by the functions below to allow for an easier
  24. -> interface to the animation system.
  25. ->>>
  26.  
  27. ->>> OBJECT newVSprite
  28. -> Object to hold information for a new VSprite.
  29. OBJECT newVSprite
  30.   image:PTR TO INT      -> Image data for the VSprite
  31.   colorSet:PTR TO INT   -> Colour array for the VSprite
  32.   wordWidth:INT         -> Width in words
  33.   lineHeight:INT        -> Height in lines
  34.   imageDepth:INT        -> Depth of the image
  35.   x:INT                 -> Initial x position
  36.   y:INT                 -> Initial y position
  37.   flags:INT             -> VSprite flags
  38.   hitMask:INT           -> Hit mask
  39.   meMask:INT            -> Me mask
  40. ENDOBJECT
  41. ->>>
  42.  
  43. ->>> OBJECT newBob
  44. -> Object to hold information for a new Bob.
  45. OBJECT newBob
  46.   image:PTR TO INT  -> Image data FOR the Bob
  47.   wordWidth:INT     -> Width in words
  48.   lineHeight:INT    -> Height in lines
  49.   imageDepth:INT    -> Depth of the image
  50.   planePick:INT     -> Planes that get image data
  51.   planeOnOff:INT    -> Unused planes to turn on
  52.   bFlags:INT        -> Bob flags
  53.   dBuf:INT          -> 1=double buf, 0=not
  54.   rasDepth:INT      -> Depth of the raster
  55.   x:INT             -> Initial x position
  56.   y:INT             -> Initial y position
  57.   hitMask:INT       -> Hit mask
  58.   meMask:INT        -> Me mask
  59. ENDOBJECT
  60. ->>>
  61.  
  62. ->>> OBJECT newAnimComp
  63. -> Object to hold information for a new animation component.
  64. OBJECT newAnimComp
  65.   routine:LONG  -> Routine called when Comp IS displayed
  66.   xt:INT        -> Initial delta offset position
  67.   yt:INT
  68.   time:INT      -> Initial timer value
  69.   cFlags:INT    -> Flags for the Comp
  70. ENDOBJECT
  71. ->>>
  72.  
  73. ->>> OBJECT newAnimSeq
  74. -> Object to hold information for a new animation sequence.
  75. OBJECT newAnimSeq
  76.   headOb:PTR TO ao      -> Common head of object
  77.   images:PTR TO INT     -> Array of Comp image data
  78.   xt:PTR TO INT         -> Arrays of initial offsets
  79.   yt:PTR TO INT
  80.   times:PTR TO INT      -> Array of initial timer values
  81.   routines:PTR TO LONG  -> Array of functions called when Comp is drawn
  82.   cFlags:INT            -> Flags for the Comp
  83.   count:INT             -> Number of Comps in sequence (= arrays size)
  84.   singleImage:INT       -> One (or count) images
  85. ENDOBJECT
  86. ->>>
  87.  
  88. ->>> PROC setupGelSys(rPort:PTR TO rastport, reserved) HANDLE
  89. -> Setup the GELs system.  After this call is made you can use VSprites, Bobs,
  90. -> AnimComps and AnimObs.  Note that this links the GelsInfo structure into the
  91. -> RastPort, and calls InitGels().  It uses information in your rastport
  92. -> object to establish boundary collision defaults at the outer edges of the
  93. -> raster.  This routine sets up for everything - collision detection and all.
  94. -> You must already have run LoadView before ReadyGelSys is called.
  95. PROC setupGelSys(rPort:PTR TO rastport, reserved) HANDLE
  96.   DEF gInfo=NIL:PTR TO gelsinfo, vsHead=NIL:PTR TO vs, vsTail=NIL:PTR TO vs
  97.   NEW gInfo, vsHead, vsTail
  98.   NEW gInfo.nextline[8], gInfo.lastcolor[8], gInfo.collhandler
  99.   gInfo.sprrsrvd:=reserved
  100.   -> Set left- and top-most to 1 to better keep items inside the display
  101.   -> boundaries.
  102.   gInfo.leftmost:=1; gInfo.topmost:=1
  103.   gInfo.rightmost:=(rPort.bitmap.bytesperrow*8)-1
  104.   gInfo.bottommost:=rPort.bitmap.rows-1
  105.   rPort.gelsinfo:=gInfo
  106.   InitGels(vsHead, vsTail, gInfo)
  107. EXCEPT
  108.   IF gInfo THEN END gInfo.nextline[8], gInfo.lastcolor[8], gInfo.collhandler
  109.   END gInfo, vsHead, vsTail
  110.   ReThrow()
  111. ENDPROC gInfo
  112. ->>>
  113.  
  114. ->>> PROC cleanupGelSys(gInfo:PTR TO gelsinfo, rPort:PTR TO rastport)
  115. -> Free all of the stuff allocated by setupGelSys().  Only call this routine if
  116. -> setupGelSys() returned successfully.  The GelsInfo structure IS the one
  117. -> returned by setupGelSys().  It also unlinks the GelsInfo from the RastPort.
  118. PROC cleanupGelSys(gInfo:PTR TO gelsinfo, rPort:PTR TO rastport)
  119.   rPort.gelsinfo:=NIL
  120.   END gInfo.nextline[8], gInfo.lastcolor[8], gInfo.collhandler,
  121.       gInfo.gelhead, gInfo.geltail
  122.   END gInfo
  123. ENDPROC
  124. ->>>
  125.  
  126. ->>> PROC makeVSprite(nVSprite:PTR TO newVSprite) HANDLE
  127. -> Create a VSprite from the information given in nVSprite.  Use freeVSprite()
  128. -> to free this GEL.
  129. PROC makeVSprite(nVSprite:PTR TO newVSprite) HANDLE
  130.   DEF vsprite=NIL:PTR TO vs, line_size, plane_size
  131.   line_size:=SIZEOF INT * nVSprite.wordWidth
  132.   plane_size:=line_size * nVSprite.lineHeight
  133.   NEW vsprite
  134.   vsprite.borderline := NewM(line_size, MEMF_CHIP)
  135.   vsprite.collmask   := NewM(plane_size, MEMF_CHIP)
  136.   vsprite.y          := nVSprite.y
  137.   vsprite.x          := nVSprite.x
  138.   vsprite.vsflags    := nVSprite.flags
  139.   vsprite.width      := nVSprite.wordWidth
  140.   vsprite.depth      := nVSprite.imageDepth
  141.   vsprite.height     := nVSprite.lineHeight
  142.   vsprite.memask     := nVSprite.meMask
  143.   vsprite.hitmask    := nVSprite.hitMask
  144.   vsprite.imagedata  := nVSprite.image
  145.   vsprite.sprcolors  := nVSprite.colorSet
  146.   vsprite.planepick  := 0
  147.   vsprite.planeonoff := 0
  148.   InitMasks(vsprite)
  149. EXCEPT
  150.   IF vsprite
  151.     Dispose(vsprite.borderline)
  152.     Dispose(vsprite.collmask)
  153.   ENDIF
  154.   END vsprite
  155.   ReThrow()
  156. ENDPROC vsprite
  157. ->>>
  158.  
  159. ->>> PROC makeBob(nBob:PTR TO newBob) HANDLE
  160. -> Create a Bob from the information given in nBob.  Use freeBob() to free this
  161. -> GEL.  A VSprite is created for this bob.  This routine properly allocates
  162. -> all double buffered information if it is required.
  163. PROC makeBob(nBob:PTR TO newBob) HANDLE
  164.   DEF bob=NIL:PTR TO bob, vsprite=NIL:PTR TO vs, rassize
  165.   rassize:=SIZEOF INT * nBob.wordWidth * nBob.lineHeight * nBob.rasDepth
  166.   NEW bob
  167.   bob.savebuffer:=NewM(rassize, MEMF_CHIP)  
  168.   vsprite:=makeVSprite([nBob.image, NIL, nBob.wordWidth, nBob.lineHeight,
  169.                         nBob.imageDepth, nBob.x, nBob.y, nBob.bFlags,
  170.                         nBob.hitMask, nBob.meMask]:newVSprite)
  171.   vsprite.planepick:=nBob.planePick
  172.   vsprite.planeonoff:=nBob.planeOnOff
  173.   vsprite.vsbob:=bob
  174.   bob.bobvsprite:=vsprite
  175.   bob.imageshadow:=vsprite.collmask
  176.   bob.bobflags:=0
  177.   bob.before:=NIL
  178.   bob.after:=NIL
  179.   bob.bobcomp:=NIL
  180.  
  181.   IF nBob.dBuf
  182.     NEW bob.dbuffer
  183.     bob.dbuffer.bufbuffer:=NewM(rassize, MEMF_CHIP)
  184.   ELSE
  185.     bob.dbuffer:=NIL
  186.   ENDIF
  187. EXCEPT
  188.   IF vsprite THEN freeVSprite(vsprite)
  189.   IF bob
  190.     IF bob.dbuffer THEN Dispose(bob.dbuffer.bufbuffer)
  191.     END bob.dbuffer
  192.     Dispose(bob.savebuffer)
  193.   ENDIF
  194.   END bob
  195.   ReThrow()
  196. ENDPROC bob
  197. ->>>
  198.  
  199. ->>> PROC makeComp(nBob:PTR TO newBob, nAnimComp:PTR TO newAnimComp) HANDLE
  200. -> Create a Animation Component from the information given in nAnimComp and
  201. -> nBob.  Use freeComp() to free this GEL.  makeComp() calls makeBob(), and
  202. -> links the Bob into an AnimComp.
  203. PROC makeComp(nBob:PTR TO newBob, nAnimComp:PTR TO newAnimComp) HANDLE
  204.   DEF compBob=NIL:PTR TO bob, aComp=NIL:PTR TO ac
  205.   NEW aComp
  206.   compBob:=makeBob(nBob)
  207.   compBob.before:=NIL
  208.   compBob.after:=NIL
  209.   compBob.bobcomp:=aComp  -> Link 'em up
  210.   aComp.animbob      := compBob
  211.   aComp.timeset      := nAnimComp.time  -> Number of ticks active
  212.   aComp.ytrans       := nAnimComp.yt    -> Offset relative to HeadOb
  213.   aComp.xtrans       := nAnimComp.xt
  214.   aComp.animcroutine := nAnimComp.routine
  215.   aComp.compflags    := nAnimComp.cFlags
  216.   aComp.timer        := 0
  217.   aComp.prevseq      := NIL
  218.   aComp.nextseq      := NIL
  219.   aComp.prevcomp     := NIL
  220.   aComp.nextcomp     := NIL
  221.   aComp.headob       := NIL
  222. EXCEPT
  223.   -> E-Note: Don't need to freeBob(compBob)...
  224.   END aComp
  225.   ReThrow()
  226. ENDPROC aComp
  227. ->>>
  228.  
  229. ->>> PROC makeSeq(nBob:PTR TO newBob, nAnimSeq:PTR TO newAnimSeq) HANDLE
  230. -> Create an Animation Sequence from the information given in nAnimSeq and
  231. -> nBob.  Use freeSeq() to free this GEL.  This routine creates a linked list
  232. -> of animation components which make up the animation sequence.  It links them
  233. -> all up, making a circular list of the prevseq and nextseq pointers.  That is
  234. -> to say, the first component of the sequence's prevseq points to the last
  235. -> component; the last component of the sequence's nextseq points back to the
  236. -> first component.  If dbuf is on, the underlying Bobs will be set up for
  237. -> double buffering.  If singleImage is non-zero, the 'images' pointer is
  238. -> assumed to point to an array of only one image, instead of an array of
  239. -> 'count' images, and all Bobs will use the same image.
  240. PROC makeSeq(nBob:PTR TO newBob, nAnimSeq:PTR TO newAnimSeq) HANDLE
  241.   DEF seq, firstCompInSeq=NIL:PTR TO ac, seqComp=NIL:PTR TO ac,
  242.       lastCompMade=NIL:PTR TO ac, image_size, nAnimComp:newAnimComp
  243.   -> Get the initial image.  This is the only image that is used if
  244.   -> nAnimSeq.singleImage is non-zero.
  245.   nBob.image:=nAnimSeq.images
  246.   image_size:=nBob.lineHeight * nBob.imageDepth * nBob.wordWidth
  247.  
  248.   -> For each comp in the sequence
  249.   FOR seq:=0 TO nAnimSeq.count-1
  250.     nAnimComp.xt      := nAnimSeq.xt[seq]
  251.     nAnimComp.yt      := nAnimSeq.yt[seq]
  252.     nAnimComp.time    := nAnimSeq.times[seq]
  253.     nAnimComp.routine := nAnimSeq.routines[seq]
  254.     nAnimComp.cFlags  := nAnimSeq.cFlags
  255.     seqComp:=makeComp(nBob, nAnimComp)
  256.     seqComp.headob:=nAnimSeq.headOb
  257.     -> Make a note of where the first component is.
  258.     IF firstCompInSeq=NIL THEN firstCompInSeq:=seqComp
  259.     -> Link the component into the list
  260.     IF lastCompMade<>NIL THEN lastCompMade.nextseq:=seqComp
  261.     seqComp.nextseq:=NIL
  262.     seqComp.prevseq:=lastCompMade
  263.     lastCompMade:=seqComp
  264.     -> If nAnimSeq.singleImage is zero, the image array has nAnimSeq.count
  265.     -> images.
  266.     IF nAnimSeq.singleImage=0
  267.       -> E-Note: image_size is in INTs so multiply up first
  268.       nBob.image:=nBob.image+(image_size*SIZEOF INT)
  269.     ENDIF
  270.   ENDFOR
  271.   -> On the last component in the sequence, set Next/Prev to make the linked
  272.   -> list a loop of components.
  273.   lastCompMade.nextseq:=firstCompInSeq
  274.   firstCompInSeq.prevseq:=lastCompMade
  275. EXCEPT
  276.   IF firstCompInSeq THEN freeSeq(firstCompInSeq, nBob.rasDepth)
  277.   ReThrow()
  278. ENDPROC firstCompInSeq
  279. ->>>
  280.  
  281. ->>> PROC freeVSprite(vsprite:PTR TO vs)
  282. -> Free the data created by makeVSprite().  Assumes images deallocated
  283. -> elsewhere.
  284. PROC freeVSprite(vsprite:PTR TO vs)
  285.   DEF line_size, plane_size
  286.   line_size:=SIZEOF INT * vsprite.width
  287.   plane_size:=line_size * vsprite.height
  288.   Dispose(vsprite.borderline)
  289.   Dispose(vsprite.collmask)
  290.   END vsprite
  291. ENDPROC
  292. ->>>
  293.  
  294. ->>> PROC freeBob(bob:PTR TO bob, rasdepth)
  295. -> Free the data created by makeBob().  It's important that rasdepth match the
  296. -> depth you passed to makeBob() when this GEL was made.  Assumes images
  297. -> deallocated elsewhere.
  298. PROC freeBob(bob:PTR TO bob, rasdepth)
  299.   DEF rassize
  300.   rassize:=SIZEOF INT * bob.bobvsprite.width * bob.bobvsprite.height * rasdepth
  301.   IF bob.dbuffer THEN Dispose(bob.dbuffer.bufbuffer)
  302.   END bob.dbuffer
  303.   Dispose(bob.savebuffer)
  304.   freeVSprite(bob.bobvsprite)
  305.   END bob
  306. ENDPROC
  307. ->>>
  308.  
  309. ->>> PROC freeComp(myComp:PTR TO ac, rasdepth)
  310. -> Free the data created by makeComp().  It's important that rasdepth match
  311. -> the depth you passed to makeComp() when this GEL was made. Assumes images
  312. -> deallocated elsewhere.
  313. PROC freeComp(myComp:PTR TO ac, rasdepth)
  314.   freeBob(myComp.animbob, rasdepth)
  315.   END myComp
  316. ENDPROC
  317. ->>>
  318.  
  319. ->>> PROC freeSeq(headComp:PTR TO ac, rasdepth)
  320. -> Free the data created by makeSeq().  Complementary to makeSeq(), this
  321. -> routine goes through the nextseq pointers and frees the Comps.  This routine
  322. -> only goes forward through the list, and so it must be passed the first
  323. -> component in the sequence, or the sequence must be circular (which is
  324. -> guaranteed if you use makeSeq()).  It's important that rasdepth match the
  325. -> depth you passed to makeSeq() when this GEL was made.   Assumes images
  326. -> deallocated elsewhere!
  327. PROC freeSeq(headComp:PTR TO ac, rasdepth)
  328.   DEF curComp:PTR TO ac, nextComp
  329.   -> Break the nextseq loop, so we get a NIL at the end of the list.
  330.   headComp.prevseq.nextseq:=NIL
  331.  
  332.   curComp:=headComp  -> Get the start of the list
  333.   WHILE curComp<>NIL
  334.     nextComp:=curComp.nextseq
  335.     freeComp(curComp, rasdepth)
  336.     curComp:=nextComp
  337.   ENDWHILE
  338. ENDPROC
  339. ->>>
  340.  
  341. ->>> PROC freeOb(headOb:PTR TO ao, rasdepth)
  342. -> Free an animation object (list of sequences).  freeOb() goes through the
  343. -> nextcomp pointers, starting at the AnimOb's headcomp, and frees every
  344. -> sequence.  It only goes forward.  It then frees the Object itself.  Assumes
  345. -> images deallocated elsewhere!
  346. PROC freeOb(headOb:PTR TO ao, rasdepth)
  347.   DEF curSeq:PTR TO ac, nextSeq
  348.   curSeq:=headOb.headcomp  -> Get the start of the list
  349.   WHILE curSeq<>NIL
  350.     nextSeq:=curSeq.nextcomp
  351.     freeSeq(curSeq, rasdepth)
  352.     curSeq:=nextSeq
  353.   ENDWHILE
  354.   END headOb
  355. ENDPROC
  356. ->>>
  357.